// let domain = 'https://app.lgwisdomunion/test/';
// let domain = 'https://app.lgwisdomunion.cn';
let domain = 'https://w.lgwisdomunion.cn';
import sign from "@/Grace6/js/sign.js";
import request from "./request";
import config from "../../config.js";
import string from '@/Grace6/js/string.js';

/**
 * 自定义请求工具
 */

// Base64编码函数
function base64encode(str) {
	return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
		return String.fromCharCode('0x' + p1);
	}));
}

// Base64解码函数
function base64decode(str) {
	return decodeURIComponent(atob(str).split('').map(function(c) {
		return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
	}).join(''));
}

export default {
	Get: function(url, data, success, fail, opt = null) {
		if (opt == null) opt = {
			showLoading: true,
			miao: 2000,
			token: true
		}
		this.Request({
			url: url,
			data: data,
			method: "GET"
		}, success, opt, fail);
	},
	Post: function(url, data, success, fail = null, opt = null) {
		if (opt == null) opt = {
			showLoading: true,
			miao: 2000,
			token: true
		}
		this.Request({
			url: url,
			data: data
		}, success, opt, fail);
	},
	PostNoToken: function(url, data, success, fail = null, opt = {
		showLoading: false,
		miao: 2500,
		token: false
	}) {
		this.Request({
			url: url,
			data: data
		}, success, opt.showLoading, opt.miao, fail);
	},

	/**
	 * 请求封装
	 * @param {Object} res
	 * @param {Object} success
	 * @param {Object} fail
	 */
	Request: function(res, success, opt = {
		showLoading: false,
		miao: 2500,
		token: true
	}, fail) {
		var that = this;
		//let baseUrl =process.env.NODE_ENV === 'development' ? '/test' :domain
		let baseUrl = config.apiUrl + "/lgzhsys/ttsx_api";
		// let baseUrl = domain;
		// // #ifdef H5
		// baseUrl = '/test';
		// // #endif
		res.data = res.data ? res.data : {};
		let method = res.method ? res.method : "POST";
		let _header = {
			"Content-Type": "application/json; charset=UTF-8",
			"Authorization": "Bearer " + uni.getStorageSync("Bearer"),
		}	
		
		if (opt.showLoading) {
			uni.showLoading({
				title: "请求中..."
			})
		}
		console.log("baseUrl: ", baseUrl);
		// console.log("request->");
		console.log("baseUrl+res.url: ", baseUrl + res.url);
		console.log("header: ", _header);
		console.log("data: ", res.data);
		console.log('method: ', method);
		uni.request({
			url: baseUrl,
			data: res,
			method: "POST",
			// url: baseUrl + res.url,
			// data: res.data,
			// method: method,
			header: _header,
			dataType: 'json',
			success: function(res) {
				uni.hideLoading();
				// console.log("success->"+res.url,res);
				console.log('接口数据：', res);
				if (res.statusCode == 200) {
					if (res.data.code == "1") {
						success(res.data);
					} else {

					
						fail(res.data);
					}

				} else {
					fail(res.data);
				}
			},
			fail: function(e) {
				uni.hideLoading();

				if (fail) fail();
			}
		})
	},
	msg(t) {
		uni.showToast({
			title: t,
			duration: 2000
		});
	},
	base64encode,
	base64decode,
	reload_login(fnc) {
		console.log("reload_login");
		let ld = uni.getStorageSync("ld");

		let url = "https://app.lgwisdomunion/APP/app/login";

		// #ifdef H5
		console.log('执行了H5')
		url = "/test/APP/app/login";
		// #endif
		let data = {
			userName: ld.u,
			password: string.base64encode(ld.p)
		}
		data["ttsxAuthSign"] = sign.sign(data).toString();

		uni.request({
			url: url,
			//url:"/test/APP/app/login",
			method: "POST",
			header: {
				'content-type': 'application/x-www-form-urlencoded'
			},
			data: data,
			success(res) {
				console.log("reload_login->res", res);
				if (res.statusCode == 200) {
					if (res.data.code == "1") {

						uni.setStorageSync("token", res.data.data.token);
						uni.setStorageSync("login", res.data.data);
						uni.setStorageSync("ttsxAuthToken", res.data.data.token);
						uni.setStorageSync("userInfo", res.data.data.userInfo);
						uni.setStorageSync("platformMall", res.data.data.platformMall)
					}
					fnc(res.data.data);
				}
			}
		})


	}
}